package com.example.yoga.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.example.yoga.entity.*;
import com.example.yoga.service.ICardCourseService;
import com.example.yoga.service.ICardService;
import com.example.yoga.service.IUserCardService;
import com.example.yoga.service.IUserService;
import com.example.yoga.util.JsonResult;
import com.example.yoga.util.PageDataResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liwenkai
 * @since 2021-02-01
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ICardService cardService;

    @Autowired
    private IUserCardService userCardService;

    @Autowired
    private ICardCourseService cardCourseService;


    /**
     * 获取系统内的用户总数
     */
    @RequestMapping(value = "/getTotalUser",method = RequestMethod.GET)
     public JsonResult<Integer> getTotalUser(){
        int totalUser = userService.getTotalUser();
        return new JsonResult<>(2000,totalUser);
    }

    /**
     * 添加用户（普通用户、教练） 注意权限问题，教练只能添加用户，管理员可以添加用户，教练，管理员
     * 管理员不能添加，系统指定
     * 根据角色进行添加用户
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public JsonResult<Void> addUser(@RequestBody User user) {
        System.out.println(user);
        //对用户进行不重名验证
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("userName", user.getUsername());
        User one = userService.getOne(userQueryWrapper);
        if (one != null) {
            return new JsonResult<>(4000, "用户名重复，请换一个用户名！");
        }
        boolean b = userService.save(user);
        if (b) {
            return new JsonResult<>(2000, "添加成功！");
        } else {
            return new JsonResult<>(2000, "添加失败！");
        }
        // Integer roleId = (Integer) session.getAttribute("roleId");
        //如果管理员，可以添加任何人,不包括管理员
//        if (roleId == 1&&user.getRoleid() != 1) {
//                boolean save = userService.save(user);
//                if (save){
//                    return new JsonResult<User>(2000,"添加成功");
//                }
//        }
        //如果是教练,只能添加普通用户
//        if (roleId == 2) {
//            if (user.getRoleid()!=1&&user.getRoleid()!=2) {
//                boolean save = userService.save(user);
//            }
//        }
        // 存值键值对 session.setAttribute(user.getUsername(),user);

    }

    /**
     * 获取所有vip的卡 getAllVipCardList
     */
    @RequestMapping("/getAllVipCardList")
    public PageDataResult getAllVipCardList(String name, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        System.out.println("*****************************");
        //不做查询了
        if (name != null) {

        }
        List<VipCardVo> list = userService.getAllVipCardList();
        System.out.println(list.toString());


        if (list.size() > 0) {
            PageInfo<VipCardVo> pageInfo = new PageInfo<>(list);
            PageDataResult pdr = new PageDataResult();
            pdr.setList(pageInfo.getList());
            pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
            return pdr;
        }
        return null;
    }


    /**
     * 查询Vip列表（带条件查询）
     */
    @RequestMapping("/getVipList")
    public PageDataResult getUserList(String name, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        if (name == null || name.length() == 0) {
            //查询全部Vip
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roleId", 3);
            List<User> userList = userService.list(queryWrapper);
            //进行分页
            PageInfo<User> pageInfo = new PageInfo<User>(userList);
            PageDataResult pdr = new PageDataResult();
            pdr.setList(pageInfo.getList());
            pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
            return pdr;
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("roleId", 3);
        wrapper.like("username", name);
        List<User> list = userService.list(wrapper);
        //进行分页
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        PageDataResult pdr = new PageDataResult();
        pdr.setList(pageInfo.getList());
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        return pdr;
    }


    /**
     * getTeaList查询教练列表（带条件查询）
     */
    @RequestMapping("/getTeaList")
    public PageDataResult getTeaList(String name, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        if (name == null || name.length() == 0) {
            //查询全部Vip
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roleId", 2);
            List<User> userList = userService.list(queryWrapper);
            //进行分页
            PageInfo<User> pageInfo = new PageInfo<User>(userList);
            PageDataResult pdr = new PageDataResult();
            pdr.setList(pageInfo.getList());
            pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
            return pdr;
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("roleId", 2);
        wrapper.like("username", name);
        List<User> list = userService.list(wrapper);
        //进行分页
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        PageDataResult pdr = new PageDataResult();
        pdr.setList(pageInfo.getList());
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        return pdr;
    }

    /**
     * addCourseToTeacher  给教练分配课程
     */
    @RequestMapping(value = "/addCourseToTeacher", method = RequestMethod.POST)
    public JsonResult<Void> addCourseToTeacher(@RequestBody CardCourse cardCourse) {
        System.out.println(cardCourse.toString());
        //首先进行判断看CardCourse里面的cardid和courseid是否为空
        if (cardCourse.getCourseid() == null || cardCourse.getCardid() == null) {
            return new JsonResult<>(4000, "分配信息不完整，请重新选择！");
        }
        //不为空，执行以下操作，看看cardcourse里面是否有要添加的这条记录，存在，则提示该教练下已有这们课程，不存在，则进行添加
        QueryWrapper<CardCourse> cardCourseQueryWrapper = new QueryWrapper<>();
        cardCourseQueryWrapper.eq("cardId", cardCourse.getCardid());
        cardCourseQueryWrapper.eq("courseId", cardCourse.getCourseid());
        CardCourse one = cardCourseService.getOne(cardCourseQueryWrapper);
        if (one != null) {
            return new JsonResult<>(4000, "该课程已经在教练的课程列表中，请勿重复添加！");
        }
        //教练没有购买时间
        boolean save = cardCourseService.save(cardCourse);
        if (save) {
            return new JsonResult<>(2000, "分配成功！");
        }
//        System.out.println(courseMv.toString()+"************************");
//        if(courseMv==null){
//            return new JsonResult<>(4000,"添加失败");
//        }
//        //判断是否已有此纪录
//        QueryWrapper<CourseMv> courseMvQueryWrapper = new QueryWrapper<>();
//        courseMvQueryWrapper.eq("mvId",courseMv.getMvid());
//        courseMvQueryWrapper.eq("courseId",courseMv.getCourseid());
//        List<CourseMv> list = courseMvService.list(courseMvQueryWrapper);
//        if (list.size()>0){
//            return new JsonResult<>(4000,"添加失败,该视频已存在");
//        }
//        boolean b = courseMvService.save(courseMv);
//        if (b){
//            return new JsonResult<>(2000,"添加成功");
//        }
//        return new JsonResult<>(4000,"添加失败");
        return new JsonResult<>(4000, "分配失败！");
    }


    /**
     * 获取所有教练
     * 返回值里要包含教练名字以及教练的cardid
     */
    @RequestMapping("/getAllTeaList")
    public JsonResult<List> getAllTeaList() {
        //查询出所有的教练列表
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("roleId", 2);
        List<User> list = userService.list(userQueryWrapper);
        List<User> list1 = new ArrayList<>();
        list1.addAll(list);
        System.out.println("这是我转移的列表" + list1);
        //查询出所有user_card表中的userid
        List<UserCard> userCardList = userCardService.list();

        //创建一个返回值对象的返回列表
        List<TeacherVo> resultList = new ArrayList<>();
        //循环这些教练是否都有cardid,若是存在没有的，给其分配一个
        if (list.size() == 0 || userCardList.size() == 0) {
            return new JsonResult<>(4000, "暂无数据");
        }

        for (int i = 0; i < list1.size(); i++) {
            for (int j = 0; j < userCardList.size(); j++) {
                if ((list1.size() > 0) && (list1.get(i).getUserid().equals(userCardList.get(j).getUserid()))) {
                    list1.remove(i);
                }
            }
        }
        //这时候list1里面只有没有卡的教练,新建卡并分配
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                //新创建一张新卡
                Card card = new Card();
                card.setCardno(UUID.randomUUID().toString().replaceAll("-", ""));
                cardService.save(card);
                //获取这个新添加的卡id
                QueryWrapper<Card> cardQueryWrapper = new QueryWrapper<>();
                cardQueryWrapper.eq("cardNo", card.getCardno());
                Card one = cardService.getOne(cardQueryWrapper);
                //创建记录user_card
                UserCard userCard = new UserCard();
                userCard.setUserid(list1.get(i).getUserid());
                userCard.setCardid(one.getCardid());
                userCardService.save(userCard);
            }
        }
        //全部写入到返回值列表中
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < userCardList.size(); j++) {
                if (list.get(i).getUserid().equals(userCardList.get(j).getUserid())) {
                    TeacherVo teacherVo = new TeacherVo();
                    teacherVo.setUserid(list.get(i).getUserid());
                    teacherVo.setUsername(list.get(i).getUsername());
                    teacherVo.setCardid(userCardList.get(j).getCardid());
                    resultList.add(teacherVo);
                }
            }
        }
        //最后，获取这些教练的cardid,并存储list里面，并返回
        return new JsonResult<>(2000, resultList);
    }

    /**
     * 根据传过来的角色id查询用户列表
     * 普通用户列表
     * 教练列表
     */
    @RequestMapping("/query/{id}")
    public JsonResult<List> selectUser(@PathVariable("id") Integer Id) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("roleId", Id);
        List<User> userList = userService.list(wrapper);
        if (userList.size() > 0) {
            return new JsonResult<>(2000, userList);
        } else {
            return new JsonResult<>(4000, "该角色下没有用户");
        }

    }

    /**
     * 查询所有用户
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public JsonResult<List> queryAll() {
        List<User> userList = userService.list();
        if (userList.size() > 0) {
            return new JsonResult<>(2000, userList);
        } else {
            return new JsonResult<>(4000, "没有用户");
        }

    }

    /**
     * 查询除了当前用户的所有用户
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/queryOthers", method = RequestMethod.GET)
    public JsonResult<List> queryOthers(Integer id) {
        System.out.println(id);
        List<User> userList = userService.list();
        for (int i = 0; i < userList.size(); i++) {
            if (id != null && id.equals(userList.get(i).getUserid())) {
                userList.remove(i);
            }
        }
        System.out.println(userList);
        if (userList.size() > 0) {
            return new JsonResult<>(2000, userList);
        } else {
            return new JsonResult<>(4000, "没有用户");
        }

    }

    /**
     * 删除用户（普通用户或者教练）
     */
    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    public JsonResult<Void> delUser(Integer id) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("userId", id);
        boolean b = userService.remove(wrapper);
        if (b) {
            return new JsonResult<>(2000, "删除成功");
        }
        return new JsonResult<>(4000, "删除失败");
    }

    /**
     * 批量删除用户
     */
    @PostMapping("/delByIds")
    public JsonResult<Void> delByIds(@RequestBody List<Integer> ids) {
        if (!ObjectUtils.isEmpty(ids)) {
            userService.removeByIds(ids);
            return new JsonResult<>(2000, "删除成功");
        }
        return new JsonResult<>(4000, "请选择要删除的用户！");
    }


    /**
     * 修改用户（普通会员或者教练）
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public JsonResult<Void> updateUser(@RequestBody User user) {
        System.out.println(user.toString() + "************************");
        //验证数据格式（邮箱和手机号）
        if (user.getUsertel() != null) {
            //手机号
            final String patternPhone = "^[1][3,4,5,7,8][0-9]{9}$";
            final Pattern pattern1 = Pattern.compile(patternPhone);
            final Matcher mat1 = pattern1.matcher(user.getUsertel());
            if (!mat1.find()) {
                return new JsonResult<>(4000, "修改失败，手机号码格式不正确！");
            }
        }
        if (user.getUseremail() != null) {
            final String patternEmail = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            final Pattern pattern = Pattern.compile(patternEmail);
            final Matcher mat = pattern.matcher(user.getUseremail());
            if (!mat.find()) {
                return new JsonResult<>(4000, "修改失败，邮箱格式不正确！");
            }
        }
        Integer userId = user.getUserid();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        boolean b = userService.update(user, wrapper);
        if (b) {
            return new JsonResult<>(2000, "修改成功");
        }
        return new JsonResult<>(4000, "修改失败");
    }

    /**
     * 修改密码
     */
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    public void updatePwd(@RequestBody PasswordVo passwordVo) {
        System.out.println(passwordVo);
    }

    /**
     * 查询个人信息
     */
    @RequestMapping("/queryPersonalInfo")
    public JsonResult<User> queryPersonalInfo(String username, HttpSession session) {
//        System.err.println(username);
//        System.err.println(session.getAttribute("卡卡"));
//        User user = (User) session.getAttribute("可可");
//        System.err.println(user);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userName", username);
        User result = userService.getOne(wrapper);
        if (result != null) {
            return new JsonResult<>(2000, result);
        }
        return new JsonResult<>(4000, "没有该用户信息");
    }

    /**
     * 导入vip
     */
    @RequestMapping("/importExcel")
    @ResponseBody
    public JsonResult<Void> importExcel(MultipartFile file) {
        if (file == null) {
            return new JsonResult<>(4000, "文件不能为空！");
        }
        ImportParams params = new ImportParams();
        try {
            List<User> list = ExcelImportUtil.importExcel(file.getInputStream(), User.class, params);
            System.out.println(list + "12312312323131321");
            //验证获取用户列表不为空
            if (list == null || list.size() == 0) {
                return new JsonResult<>(4000, "表格不能为空！");
            }
            //获取所有Vip用户
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("roleId", 3);
            List<User> userList = userService.list(userQueryWrapper);
            System.out.println(userList);
            //遍历所有vip用户，看要插入的表中是否有同名用户
            for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < userList.size(); j++) {
                    if (list.get(i).getUsername().trim().equals(userList.get(j).getUsername().trim())) {
                        System.out.println("相同下标" + i);
                        userService.update(list.get(i), new QueryWrapper<User>().eq("userId", userList.get(j).getUserid()));
                        list.remove(i);
                        if(list.size()==0){
                            return new JsonResult<>(4000, "导入文件的记录已经全部存在！");
                        }
                    }
                }
            }
            userService.saveBatch(list);
            System.out.println(list);
            return new JsonResult<>(2000, "excel文件导入成功！新增记录"+list.size()+"条");
        } catch (NoSuchElementException e) {
            return new JsonResult<>(4000, "excel文件不能为空");
        } catch (Exception e) {
            return new JsonResult<>(4000, "导入异常！");
        }
    }

    /**
     * importCourseVip  将vip名单导入
     */
    @RequestMapping("/importCourseVipExcel")
    @ResponseBody
    public JsonResult<Void> importCourseVipExcel(MultipartFile file, @RequestParam Integer courseid) {
        System.out.println(courseid);
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        try {
            List<User> list = ExcelImportUtil.importExcel(file.getInputStream(), User.class, params);
            System.out.println(list);
            // System.out.println(list+"12312312323131321");
            //验证获取用户列表不为空
            if (list == null || list.size() == 0) {
                return new JsonResult<>(4000, "导入失败！表格不能为空！");
            }
            //获取所有Vip用户
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("roleId", 3);
            List<User> userList = userService.list(userQueryWrapper);
            System.out.println(userList);
            ////遍历所有vip用户，看要插入的表中的用户是否存在，如果有一个不存在，则提示导入失败（无效）
            //取出所有vip名单，将导入文件的所有vip与数据库中的vip进行对比，存在并计数；若有不存在的，则计数值小于导入的vip数量
            //同时记录要插入用户的列表（这里记录要插入用户的id,方便后续操作）
            int count = 0;
            List<Integer> userIdList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < userList.size(); j++) {
                    if (list.get(i).getUsername().trim().equals(userList.get(j).getUsername().trim())) {
//                        System.out.println("相同下标"+i);
//                        userService.update(list.get(i), new QueryWrapper<User>().eq("userId",userList.get(j).getUserid()));
//                        list.remove(i);
                        userIdList.add(userList.get(j).getUserid());
                        count++;
                    }
                }
            }
            if (count < list.size()) {
                System.out.println("导入失败！有" + (list.size() - count) + "记录不合法！本系统没有这些用户");
                return new JsonResult<>(4000, "导入失败！有" + (list.size() - count) + "记录不合法！本系统没有这些用户");
            }
            //导入的列表在数据库表中都存在,判断这些用户是否都有卡（有卡才能购买课程）
            //有卡，才能参加课程，也就是说没卡不能导入（我们导入的是已存在的用户，并不是未存在的用户）
            //获取所有用户和卡关联的记录
            int count2 = 0;
            //我们这里使用上述遍历之后得到的用户Id列表userIdList
            List<UserCard> userCards = userCardService.list();
            if (userCards.size() == 0) {
                System.out.println("用户卡关系表数据为空！");
                return new JsonResult<>(4000, "导入失败！用户卡关系表数据为空！");
            }
            //存cardId的list
            List<Integer> cardIdList = new ArrayList<>();
            for (int i = 0; i < userIdList.size(); i++) {
                for (int j = 0; j < userCards.size(); j++) {
                    if (userIdList.get(i).equals(userCards.get(j).getUserid())) {
                        count2++;
                        cardIdList.add(userCards.get(j).getCardid());
                    }
                }
            }
            //判断导入用户的数量是否都存在
            if (userIdList.size() != count2) {
                System.out.println("存在不合法用户！");
                return new JsonResult<>(4000, "导入失败！存在无卡用户！");
            }
            //看看这些excel中的用户，是否已经有在这个课程下的了，如果有，则剔除该用户，添加别的没有关联的用户
            List<CardCourse> cardCourseList = cardCourseService.list();//数据库中的cardcourse关联表
            for (int i = 0; i < cardIdList.size(); i++) {
                for (int j = 0; j < cardCourseList.size(); j++) {
                    if (cardIdList.get(i).equals(cardCourseList.get(j).getCardid())&&cardCourseList.get(j).getCourseid().equals(courseid)) {
//                        这个问题有点难度，不太容易解决，数组下标异常，无法使remove方法动态改变数组的大小
//                        if () {
                            //表示这条课程与卡的记录存在，则不再重复添加，将其从卡id从这个cardId列表中移除
                            cardIdList.remove(i);
                            if (cardIdList.size()==0){
                                return new JsonResult<>(4000, "导入数据已存在！请不要重复导入");
                            }
//                        }
                    }
                }
            }
            //根据上面的存cardId的list
//            QueryWrapper<UserCard> userCardQueryWrapper = new QueryWrapper<>();
//            userCardQueryWrapper.in("userId",)
//            if (cardIdList.size() == 0) {
//                return new JsonResult<>(4000, "导入数据已存在！请不要重复导入");
//            }
            System.out.println("*************************"+cardIdList.toString());
            for (int i = 0; i < cardIdList.size(); i++) {
                CardCourse cardCourse = new CardCourse();
                cardCourse.setCardid(cardIdList.get(i));
                cardCourse.setCourseid(courseid);
                cardCourse.setBuydate(new Date());
                cardCourseService.save(cardCourse);
            }

            //   userService.saveBatch(list);
            //以上是进行vip保存到user表中，但还要将关联数据记录填充到usercard表中
//            for (int i = 0; i < userList.size() ; i++) {
//                Card card = new Card();
//                card.setCardno(UUID.randomUUID().toString().replaceAll("-",""));
//
//            }
            System.out.println(list);
            return new JsonResult<>(2000, "excel文件导入成功！新增"+cardIdList.size()+"条记录");
        } catch (NoSuchElementException e) {
            return new JsonResult<>(4000, "excel文件不能为空");
        } catch (Exception e) {
            //System.out.println(e);
            return new JsonResult<>(4000, "导入失败！有课程下存在的会员");
        }
    }


    /**
     * 导出
     */
    @RequestMapping(value = "/exportAllUser", method = RequestMethod.GET)
    public void exportAllUser(HttpServletResponse response, Integer type) throws IOException {
        //判断导出学生还是教练
        System.out.println("你要导出的类型时" + type);
        if (type == 2) {
            //导出的是教练
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("roleId", type);
            List<User> list = userService.list(wrapper);
            String fileName = "allteacher.xls";
            try {
                response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                //导出
                Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, list);
                workbook.write(response.getOutputStream());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                return;
            }
        }
        if (type == 3) {
            //导出的是会员
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("roleId", type);
            List<User> list = userService.list(wrapper);
            String fileName = "allvip.xls";
            try {
                response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                //导出
                Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, list);
                workbook.write(response.getOutputStream());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                return;
            }
        }
    }


}

